package com.example.etwzgzxt.view.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.etwzgzxt.R;
import com.example.etwzgzxt.tool.CountDownButtonHelper;
import com.example.etwzgzxt.tool.HttpHelper;
import com.example.etwzgzxt.tool.InfoApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

public class RegisterActivity extends AppCompatActivity {

    public static final int GET_VCODE = 0;

    public static final int REGISTER = 1;

    private View.OnClickListener navListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RegisterActivity.this.finish();
        }
    };

    private Button btnGetVCode;
    private EditText etPNum, etPwd, etVCode;
    private String VCodeId;
    private String access_token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regeister);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("注册账户");
            setSupportActionBar(toolbar);
            toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow_back);
            toolbar.setNavigationOnClickListener(navListener);
        }
        initView();
    }

    public void initView() {
        etPNum = (EditText) findViewById(R.id.etPhoneNumber);
        etPwd = (EditText) findViewById(R.id.etPassword);
        etVCode = (EditText) findViewById(R.id.etVCode);
        btnGetVCode = (Button) findViewById(R.id.btnGetVCode);
        btnGetVCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etPNum.getText().toString().length() != 11) {
                    return;
                }
                new BackgroundTask(GET_VCODE).execute();
                CountDownButtonHelper helper = new CountDownButtonHelper(btnGetVCode,
                        "发送验证码", 60, 1);
                helper.start();
            }
        });
    }

    public void btnRegister(View view) {
        new BackgroundTask(REGISTER).execute();
    }

    public void parseResult(String result, int type) {
        if (result == null) {
            Toast.makeText(this, "请检查网络设置", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            switch (jsonObject.getInt("code")) {
                case 200:
                    if (type == GET_VCODE) {
                        VCodeId = jsonObject.getString("validatecodeid");
                        Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(this, InfoUpdateActivity.class));
                        finish();
                    }
                    break;
                case 403:
                    Toast.makeText(this, "Not Acceptable", Toast.LENGTH_SHORT).show();
                    break;
                case 406:
                    Toast.makeText(this, "Forbidden", Toast.LENGTH_SHORT).show();
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public class BackgroundTask extends AsyncTask<Void, Void, String> {

        int type;

        public BackgroundTask(int type) {
            this.type = type;
        }

        @Override
        protected String doInBackground(Void... params) {
            if (type == GET_VCODE) {
                return getVCode();
            } else {
                return register();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            parseResult(result, type);
        }
    }

    public String getVCode() {
        String baseURL = ((InfoApplication)getApplicationContext()).getBasehost()+"/user/getValidateCode.do";
        NameValuePair pair = new BasicNameValuePair("phonenumber", etPNum.getText().toString());
        List<NameValuePair> pairList = new ArrayList<>();
        pairList.add(pair);
        return new HttpHelper().post(baseURL, pairList);
    }

    public String register() {
        String baseURL = ((InfoApplication)getApplicationContext()).getBasehost()+"/user/register.do";
        NameValuePair pair[] = new NameValuePair[4];
        pair[0] = new BasicNameValuePair("phonenumber", etPNum.getText().toString());
        pair[1] = new BasicNameValuePair("validatecode", etVCode.getText().toString());
        pair[2] = new BasicNameValuePair("validatecodeid", VCodeId);
        pair[3] = new BasicNameValuePair("password", etPwd.getText().toString());
        List<NameValuePair> pairList = new ArrayList<>();
        Collections.addAll(pairList, pair);
        return new HttpHelper().post(baseURL, pairList);
    }
}
